package com.zjj.sc.movie.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.feign.FeignClientsConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.zjj.sc.movie.dto.User;

import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.auth.BasicAuthRequestInterceptor;
import feign.codec.Decoder;
import feign.codec.Encoder;

@RestController
@Import(FeignClientsConfiguration.class)
public class MovieController {
	
	private UserFeignClient userFeignClient;
	private UserFeignClient adminFeignClient;
	
	@Autowired
	public MovieController (Decoder decoder,Encoder encoder,Client client,Contract contract) {
		this.userFeignClient = Feign.builder()
									.client(client)
									.encoder(encoder)
									.decoder(decoder)
									.contract(contract)
									.requestInterceptor(new BasicAuthRequestInterceptor("user", "pwd1"))
									.target(UserFeignClient.class,"http://microservice-8-provider-user-with-auth");
		this.adminFeignClient = Feign.builder()
									.client(client)
									.encoder(encoder)
									.decoder(decoder)
									.contract(contract)
									.requestInterceptor(new BasicAuthRequestInterceptor("admin", "pwd2"))
									.target(UserFeignClient.class,"http://microservice-8-provider-user-with-auth");
	}
	
	@GetMapping("/user-user/{id}")
	public User findByIdUser(@PathVariable Long id) {
		System.out.println("--------get user by feignClient = " + userFeignClient + "-------");
		return this.userFeignClient.findById(id);
	}
	
	@GetMapping("/user-admin/{id}")
	public User findByIdAdmin(@PathVariable Long id) {
		System.out.println("--------get user by feignClient = " + adminFeignClient + "-------");
		return this.adminFeignClient.findById(id);
	}
	

}
